﻿using ML.Entity;
using ML.IBusiness;
using System.Web.Mvc;

namespace ML.Web.Controllers
{
    /// <summary>
    /// 订单控制器
    /// </summary>
    [Login]
    public class OrderController : BaseController
    {
        private readonly IOrderBusiness orderBusiness;
        public OrderController(IOrderBusiness _orderBusiness)
        {
            orderBusiness = _orderBusiness;
        }

        /// <summary>
        /// 订单管理页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 个人商户—订单管理页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult UserIndex()
        {
            return View();
        }

        /// <summary>
        /// 根据查询条件查询订单信息
        /// </summary>
        /// <param name="orderSearch">订单查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult List(OrderSearch orderSearch)
        {
            var data = orderBusiness.List(orderSearch);
            return JsonDateTime(data.data);
        }

        /// <summary>
        /// 根据查询条件查询商户下订单信息
        /// </summary>
        /// <param name="orderSearch">订单查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult ListUser(OrderSearch orderSearch)
        {
            var merchantId = UserHelper.GetCurrentUser().MerchantId;
            if (string.IsNullOrEmpty(merchantId))
            {
                merchantId = "-1";
            }
            orderSearch.merchantid_search = merchantId;
            var data = orderBusiness.List(orderSearch);
            return JsonDateTime(data.data);
        }

        /// <summary>
        /// 根据订单Id查询订单信息
        /// </summary>
        /// <param name="id">订单Id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetDetailById(string id)
        {
            var data = orderBusiness.GetDetailById(id);
            return JsonDateTime(data);
        }

        /// <summary>
        /// 修改订单表状态
        /// </summary>
        /// <param name="Id">订单Id</param>
        /// <param name="State">(1:接单,2:取消)</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult UpdateState(string Id, int State)
        {
            var data = orderBusiness.UpdateState(Id, State);
            return Json(data);
        }
    }
}